#include <windows.h>
#include "Display_eBox2300.h"
#include "regmgr.h"

//	"Resolution"=dword:0 	; 0:640x480; 1:800x600; 2:1024x768; 3:1280x1024
//	"ColorDepth"=dword:10
//	"RefreshRate"=dword:3C	;3C:60Hz, 46:70Hz, 55:85Hz

TCHAR *eBox2300Resolutions[116] = {
	TEXT("640x480"),
	TEXT("800x600"),
	TEXT("1024x768"),
	TEXT("1280x1024"),
	NULL
};

Display_eBox2300::Display_eBox2300()
{
}


Display_eBox2300::~Display_eBox2300()
{
}

BOOL Display_eBox2300::IsSupported()
{

	BOOL rez = FALSE;
	HKEY hKey;

	if ( 0 == RegOpenKeyEx(HKEY_LOCAL_MACHINE, EBOX2300_IS_SUPPORTED,0,0, &hKey) )
	{
		rez = TRUE;
	}

	if ( hKey != NULL )
	{
		CloseRegistryKey(hKey);
	}

	return rez;
}

BOOL Display_eBox2300::ModeIsSupported( DWORD width, 
			                       DWORD height,
						  		   DWORD colorDepth,
								   DWORD freq )
{
	TCHAR settings[100]={0};
	_stprintf(settings, TEXT("%dx%d"),width, height);

	switch ( freq ) 
	{
		case 60:
		case 70:
		case 85: break;
		default: return FALSE;
	}
	
	for ( int i = 0; eBox2300Resolutions[i] != NULL; i++ )
	{
		if ( _tcsicmp(eBox2300Resolutions[i], settings) == 0 )
		{
			return TRUE;
		}
	}

	return FALSE;
}

BOOL Display_eBox2300::TestModeIsSupported()
{
	return FALSE;
}

BOOL Display_eBox2300::DisplayTest(DWORD width, 
			                       DWORD height,
							       DWORD colorDepth,
							       DWORD freq )
{
	return TRUE;    
}


void Display_eBox2300::DisplayUpdate(DWORD width, 
								DWORD height,
							    DWORD colorDepth,
							    DWORD freq )
{
	TCHAR settings[100]={0};
	_stprintf(settings, TEXT("%dx%d"),width, height);

	for (int i = 0; eBox2300Resolutions[i] != NULL; i++ )
	{
		if ( _tcsicmp(eBox2300Resolutions[i], settings) == 0 )
		{
			HKEY hKey;

			if ( 0 == RegOpenKeyEx(HKEY_LOCAL_MACHINE, EBOX2300_DISPLAY_SETTINGS,0,0, &hKey) )
			{
				RegSetValueEx( hKey, EBOX2300_DISPLAY_RESOLUTION ,0, REG_DWORD, (BYTE*)&i   , sizeof(DWORD));
				RegSetValueEx( hKey, EBOX2300_DISPLAY_REFRESHRATE,0, REG_DWORD, (BYTE*)&freq, sizeof(DWORD));
			}


			if ( hKey != NULL )
			{
				CloseRegistryKey(hKey);
			}

			return;
		}
	}
}